﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Principal;
using System.Web;

using Happy.Security.Principal;

namespace Happy.Web.Mvc.Security
{
    /// <summary>
    /// 幸福授权。
    /// </summary>
    public static class HappyAuthorization
    {
        /// <summary>
        /// 存储凭证的会话键。
        /// </summary>
        public static readonly string CURRENT_PRINCIPAL_SESSION_KEY = "HAPPY_CURRENT_PRINCIPAL";

        /// <summary>
        /// 设置凭证。
        /// </summary>
        public static void SetPrincipal(IPrincipal principal)
        {
            HttpContext.Current.Session[CURRENT_PRINCIPAL_SESSION_KEY] = principal;
        }

        /// <summary>
        /// 获取凭证。
        /// </summary>
        public static IPrincipal GetPrincipal()
        {
            if (HttpContext.Current.Session[CURRENT_PRINCIPAL_SESSION_KEY] != null)
            {
                return HttpContext.Current.Session[CURRENT_PRINCIPAL_SESSION_KEY] as IPrincipal;
            }

            return HappyPrincipal.UnAuthenticatedPrincipal;
        }
    }
}
